package learn_concurrent._7;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class T5 {
	static final Logger logger = LoggerFactory.getLogger(T5.class);

	static final ReentrantLock lock = new ReentrantLock();

	public static void main(String[] args) {
		Thread t1 = new Thread(() -> {
			logger.info("想要获得锁");
			try {
				if (!lock.tryLock(3, TimeUnit.SECONDS)) {
					logger.info("获得不到锁，算了吧");
					return;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
				return;
			}

			try {
				logger.info("获得锁");
			} finally {
				lock.unlock();
			}
		});

		lock.lock();
		t1.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		lock.unlock();

	}

}
